#!/bin/sh

usage()
{
    echo "Usage :"
    echo "invertVectormap.sh <output basename> <output CT directory> <input vectormap>"
    exit 1
}

wrong()
{
        echo "Something went wrong:"
        if [ "x$1" != "x" ]; then
                echo "$1"
        fi
        exit 1
}

n=$#

if [ "x${n}" != "x3" ]; then
    usage
fi

outbase=$1
outCt=$2
inbase=$3

if [ ! -e "${inbase}.mhd" ]; then
	wrong "${inbase} mhd file does not exist"
fi

if [ ! -e "${inbase}.raw" ]; then
	wrong "${inbase} raw file does not exist"
fi

if [ ! -e "${outCt}" ]; then
	wrong "${outCt} directory does not exist"
fi

echo "Creating mhd : ${outbase}.mhd"
createMhd ${outbase} ${outCt}

if [ ! -e "${outbase}.mhd" ]; then
	wrong "${outbase}.mhd file does not exist"
fi

echo "Creating vectormap : ${outbase}.raw"
invertVectormap ${outbase} ${inbase}

if [ ! -e "${outbase}.raw" ]; then
	wrong "${outbase}.raw file does not exist"
fi
